{{!
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
}}

{{multiple-database-search-bar databases=alldatabases selectedMultiDb=selectedMultiDb changeDbHandler="changeDbHandler" }}

<div class="clearfix col-md-9 query-editor-panel">
  <div class="tab-results">
    <div class="query-editor-container">
      <div class="row query-editor" style="position:relative">
        <span class="expand-button" {{action "expandQueryEdidorPanel" }} >
          {{fa-icon "expand"}}
        </span>
        {{query-editor query=currentQuery updateQuery='updateQuery' }}
      </div>
      <div class="row query-editor-controls">
        {{#if isJobCreated}}
          {{#if isJobCancelled}}
             <button class="btn btn-warning" disabled >{{fa-icon "ban"}} Stopping</button>
          {{else}}
            <button class="btn btn-warning" {{action "stopQuery" }} >{{fa-icon "ban"}} Stop</button>
          {{/if}}
        {{else}}
          <button class="btn btn-success" {{action "executeQuery" }} disabled={{ worksheet.isQueryRunning}} >{{fa-icon "check"}} Execute</button>
        {{/if}}

        <button class="btn btn-default" {{action "openWorksheetModal" }}>{{fa-icon "save"}} Save As</button>
        <div class="btn-group">
          <button class="btn btn-default" type="button" data-toggle="dropdown" disabled={{ not allUDFList.length }}>Insert UDF
            <span class="caret"></span></button>
            <ul class="dropdown-menu">
              {{#each fileResourceList as |fileResource|}}
                {{fileresource-item fileResource=fileResource createQuery='createQuery'}}
              {{/each}}
            </ul>
        </div>
        <button class="btn btn-default" {{action "visualExplainQuery" }} disabled={{ worksheet.isQueryRunning}}>{{fa-icon "link"}} Visual Explain</button>

        {{#if worksheet.isQueryRunning}}
          {{fa-icon "spinner fa-1-5 middle-align" spin=true}}
        {{/if}}

      </div>
    </div>

    {{#tabs-pane tabs=tabs inverse=true as |tab|}}
      {{tabs-item tab=tab tabs=tabs}}
    {{/tabs-pane}}

    {{outlet}}
  </div>
</div>

<div class="col-md-3 database-panel">
  <div class="database-container">
    <div class="row">
    <div class="panel-group database-panel" id="db_accordion" role="tablist">
      {{#each selectedTablesModels as |tableModel|}}
        <div class="panel panel-default">
          <div class="panel-heading" role="tab" id={{concat 'db_heading_' tableModel.dbname}}>
            <h4 class="panel-title">
              <a role="button" data-i-toggle="collapse" data-i-parent="#db_accordion"
                 href="javascript:void(0)" {{action 'showTables' tableModel.dbname }}
                 aria-controls={{concat 'db_body_' tableModel.dbname}}>
                {{ tableModel.dbname }} {{#if (eq tableModel.dbname worksheet.selectedDb)}} {{fa-icon "check"}}  {{/if}}
              </a>
              <span class="pull-right">Tables({{tableModel.tables.length}})</span>
            </h4>
          </div>
          <div id={{concat 'db_body_' tableModel.dbname}} class="db-tables collapse
               panel-collapse {{if (eq tableModel.dbname worksheet.selectedDb) 'in'}}" role="tabpanel"
          aria-labelledby={{concat 'db_heading_' tableModel.dbname}}>
          <div class="panel-body">
            {{#if tableModel.tables.length }}
              {{#list-filter header="tables" items=tableModel.tables
              placeholder="Search Tables"
              as |filteredItems|}}
                {{#list-group class="table-list" items=filteredItems as |item|}}
                  {{list-item item=item itemClicked="tableSelected"}}
                {{/list-group}}
              {{/list-filter}}
            {{else}}
              <div class="empty">No Table found.</div>
            {{/if}}
          </div>
        </div>
      </div>
      {{/each}}
    </div>
  </div>
</div>
</div>

{{#if showWorksheetModal}}
  {{#modal-dialog translucentOverlay=true clickOutsideToClose=true container-class="modal-dialog"}}
    <div class="modal-content">
      <div class="modal-header">
        {{fa-icon "minus fa-lg"}} Confirm
      </div>
      <div class="modal-body">

        <p class="lead">{{fa-icon "save fa-lg"}} Do you want to save the worksheet?</p>

        <div class="form-horizontal">
          <div class="form-group">
            <label for="title" class="col-sm-2 control-label">Title</label>
            <div class="col-sm-10">
              {{input type="text" class="form-control" id="worksheet-title" placeholder="Title" value=worksheetTitle }}
            </div>
          </div>
          {{#if worksheetModalSuccess }}
            <div class="text-success">Successfully Saved.</div>
          {{/if}}
          {{#if worksheetModalFail }}
            <div class="text-danger">Error</div>
          {{/if}}
        </div>
      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-default" {{action "closeWorksheetModal"}}>{{fa-icon "close"}} Reject</button>
        <button type="button" class="btn btn-success" disabled={{not worksheetTitle}} {{action "saveWorksheetModal"}}>{{fa-icon "check"}} Save</button>
      </div>
    </div>
  {{/modal-dialog}}
{{/if}}

{{#if showWorksheetRenameModal}}
  {{#modal-dialog translucentOverlay=true clickOutsideToClose=true container-class="modal-dialog  modal-sm"}}
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Renaming worksheet</h4>
      </div>
      <div class="modal-body">
        <div class="form-horizontal">
          <div class="form-group">
            <label for="title" class="col-sm-2 control-label">Title</label>
            <div class="col-sm-10">
              {{input type="text" class="form-control" id="worksheet-title" placeholder="Title" value=worksheetTitle }}
            </div>
          </div>
          {{#if renameWorksheetModalSuccess }}
            <div class="text-success">Renamed successfully.</div>
          {{/if}}
          {{#if renameWorksheetModalFail }}
            <div class="text-danger">Error while renaming.</div>
          {{/if}}
        </div>
      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-default" disabled={{not worksheetTitle}} {{action "renameWorksheetModal"}}>{{fa-icon "check"}} Save</button>
        <button type="button" class="btn btn-default" {{action "closeRenameWorksheetModal"}}>{{fa-icon "close"}}Cancel
        </button>
      </div>
    </div>
  {{/modal-dialog}}
{{/if}}
